package org.takeback.chat.service;

import com.google.common.collect.ImmutableMap;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.apache.commons.collections.map.HashedMap;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.takeback.chat.entity.GcRoom;
import org.takeback.chat.entity.GcRoomProperty;
import org.takeback.chat.store.room.RoomStore;
import org.takeback.dao.BaseDAO;
import org.takeback.service.BaseService;

@Service
public class RoomService extends BaseService {
	@Autowired
	RoomStore roomStore;
	private static int ONCE_FETCH_COUNT = 5;

	@Transactional(readOnly = true)
	public List<GcRoom> getRooms(int pageNo, Map<String, Object> params) {
		return this.dao.find(GcRoom.class, params, ONCE_FETCH_COUNT, pageNo, "hot desc,createdate desc");
	}

	@Transactional(readOnly = true)
	public List<GcRoom> getActivedRooms() {
		return this.dao.findByHql("from GcRoom a where a.status != '9' order by a.hot desc,a.createdate desc", null);
	}

	@Transactional(readOnly = true)
	public Map<String, Object> getRoomProperties(String roomId) {
		Map<String, Object> param = new HashMap();
		param.put("roomId", roomId);
		List<GcRoomProperty> list = this.dao.findByHql("from GcRoomProperty a where roomId=:roomId order by id asc ",
				param);
		Map<String, Object> res = new HashMap();
		for (GcRoomProperty prop : list) {
			res.put(prop.getConfigKey(), prop.getConfigValue());
		}
		return res;
	}

	@Transactional(readOnly = true)
	public int getUserRoomCount(Integer userId) {
		return (int) this.dao.count(GcRoom.class, ImmutableMap.of("owner", userId));
	}

	@Transactional(readOnly = true)
	public List<GcRoom> getUserRooms(Integer userId, int pageSize, int pageNo) {
		return this.dao.findByHqlPaging("from GcRoom where owner=:owner", ImmutableMap.of("owner", userId), pageSize,
				pageNo);
	}

	@Transactional
	public void initRoomStatus() {
		this.dao.executeUpdate("update GcRoom set status ='0' ", new HashedMap());
		List<GcRoom> list = this.dao.findByHql("from GcRoom");
		for (GcRoom rm : list) {
			this.roomStore.reload(rm.getId());
		}
	}

	@Transactional(readOnly = true)
	public List<Map<String, String>> getRoomProps(String roomId) {
		List<GcRoomProperty> list = this.dao.findByHql("from GcRoomProperty where roomId=:roomId",
				ImmutableMap.of("roomId", roomId));
		if ((list == null) || (list.isEmpty())) {
			return new ArrayList();
		}
		List<Map<String, String>> props = new ArrayList(list.size());
		for (GcRoomProperty gcRoomProperty : list) {
			String alias = StringUtils.isEmpty(gcRoomProperty.getAlias()) ? gcRoomProperty.getConfigKey()
					: gcRoomProperty.getAlias();
			Map<String, String> map = ImmutableMap.of("key", gcRoomProperty.getConfigKey(), "value",
					gcRoomProperty.getConfigValue(), "alias", alias);
			props.add(map);
		}
		return props;
	}
	
	//改变龙虎合房间是否可以下注的状态
	@Transactional
	public void updateLHHConfig(String value, String configKey) {
		this.dao.executeUpdate("update GcRoomProperty set configValue =:status where roomId='LHH0001' and configKey= :configKey", ImmutableMap.of("status", value, "configKey", configKey));
		List<GcRoom> list = this.dao.findByHql("from GcRoom where id='LHH0001'");
		for (GcRoom rm : list) {
			this.roomStore.reload(rm.getId());
		}
	}
}
